package cn.wwxula.common.util;

import java.io.*;

import cn.wwxula.common.properties.ali.AliOssProperties;
import com.aliyuncs.exceptions.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author liyang23
 * @date 2024/12/10 09:09
 **/
@Slf4j
@Component
public class AliUtil {
    @Resource
    private AliOssProperties aliOssProperties;

    /************************* OSS *************************/
    private OSS getOssClient() throws com.aliyuncs.exceptions.ClientException {
        // 创建 OSSClient 实例
        EnvironmentVariableCredentialsProvider credentialsProvider =
                CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        return OSSClientBuilder.create()
                .endpoint(aliOssProperties.getEndpoint())
                .credentialsProvider(credentialsProvider)
                .region(aliOssProperties.getRegion())
                .build();
    }

    /**
     * 文件上传
     *
     * @param bytes      待上传文件的字节数据
     * @param objectName 待上传文件的文件名称
     * @return 阿里云OSS的存储地址
     */
    public String upload(byte[] bytes, String objectName) throws ClientException {
        return upload(new ByteArrayInputStream(bytes), objectName);
    }

    /**
     * 文件上传
     *
     * @param inputStream 待上传文件的数据输入流
     * @param objectName  待上传文件的文件名称
     * @return 阿里云OSS的存储地址
     */
    public String upload(InputStream inputStream, String objectName) throws ClientException {
        OSS ossClient = getOssClient();
        try {
            ossClient.putObject(aliOssProperties.getBucketName(), objectName, inputStream);

            //文件访问路径规则 https://bucketName.endpoint/ObjectName
            StringBuilder stringBuilder = new StringBuilder("https://");
            stringBuilder
                    .append(aliOssProperties.getBucketName())
                    .append(".")
                    .append(aliOssProperties.getEndpoint())
                    .append("/")
                    .append(objectName);
            log.info("文件上传到:{}", stringBuilder);

            return stringBuilder.toString();
        } finally {
            ossClient.shutdown();
        }
    }

    /**
     * 文件上传
     *
     * @param inputStream 待上传文件的数据输入流
     * @param objectName  待上传文件的文件名称
     * @return 阿里云OSS的存储地址
     */
    public String upload(InputStream inputStream, String bucketName, String objectName) throws ClientException {
        OSS ossClient = getOssClient();
        try {
            ossClient.createBucket(bucketName);
            log.info("创建Bucket {} 成功", bucketName);
            ossClient.putObject(aliOssProperties.getBucketName(), objectName, inputStream);
            log.info("上传文件 {} 成功", objectName);

            //文件访问路径规则 https://bucketName.endpoint/ObjectName
            StringBuilder stringBuilder = new StringBuilder("https://");
            stringBuilder
                    .append(bucketName)
                    .append(".")
                    .append(aliOssProperties.getEndpoint())
                    .append("/")
                    .append(objectName);

            log.info("文件上传到:{}", stringBuilder);

            return stringBuilder.toString();
        } finally {
            ossClient.shutdown();
        }
    }

    public void deleteObject(String bucketName, String objectName) throws ClientException {
        OSS ossClient = getOssClient();
        try {
            ossClient.deleteObject(bucketName, objectName);
            log.info("删除文件 {} 成功", objectName);
        } finally {
            ossClient.shutdown();
        }
    }

    public void deleteBucket(String bucketName) throws ClientException {
        OSS ossClient = getOssClient();
        try {
            ossClient.deleteBucket(bucketName);
            log.info("删除Bucket {} 成功", bucketName);
        } finally {
            ossClient.shutdown();
        }
    }
}